/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.javahispano.javacup.tacticas_aceptadas;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import org.javahispano.javacup.modelo.TacticaDetalle;

/**
 *
 * @author fabian
 */
public class Sherlock {

    private static String path = "C:\\Documents and Settings\\fabian\\Escritorio\\javacup.proyecto.netbeans.1.7.2\\javacup2009\\src\\org\\javahispano\\javacup\\tacticas_aceptadas";
    private static String packagePath = "org.javahispano.javacup.tacticas_aceptadas";

    static class Registro implements Comparable {

        String origen;
        String destino;
        int porcentaje;
        int nivel;
        Class clase;

        public Registro(String origen, String destino, int porcentaje) {
            this.origen = origen;
            this.destino = destino;
            this.porcentaje = porcentaje;
            File f = new File(path + destino);
            try {
                String s = packagePath + origen.replaceAll("\\\\", ".");
                if (s.endsWith(".java")) {
                    s = s.substring(0, s.length() - 5).trim();
                }
                clase = Class.forName(s);
            } catch (Exception e) {
                System.out.println(f+" "+e.getMessage());
            }
            this.nivel = porcentaje * (int) f.length() / 100;
        }

        @Override
        public int compareTo(Object o) {
            if (o instanceof Registro) {
                return nivel - ((Registro) o).nivel;
            }
            return 0;
        }

        @Override
        public String toString() {
            return "[" + nivel + "]" + porcentaje + "% " + destino + " -> " + origen;
        }
    }

    private LinkedList<File> getFiles(String path) {
        LinkedList<File> l = new LinkedList<File>();
        File[] hijos = new File(path).listFiles();
        if (hijos == null) {
            System.out.println(path);
        } else {
            for (File f : hijos) {
                if (f.isDirectory()) {
                    l.addAll(getFiles(f.getAbsolutePath()));
                } else {
                    if (f.getAbsolutePath().endsWith(".java") && !f.getAbsolutePath().endsWith("Detalle.java"))
                    l.add(f);
                }
            }
        }
        return l;
    }

    public static void main(String[] args) throws IOException {

        Sherlock sh = new Sherlock();

        LinkedList<File> files = sh.getFiles(path);
        StringBuffer command = new StringBuffer();
        command.append("sherlock -o resultados ");
        for (File f : files) {
            command.append("\""+f.getAbsolutePath().substring(88) + "\"");
        }
        System.out.println(command);
        Runtime.getRuntime().exec(command.toString());
        File f = new File("resultados");
        int size = (int) f.length();
        System.out.println(f);
        FileInputStream fis = new FileInputStream(f);
        byte[] b = new byte[size];
        fis.read(b);
        String result = new String(b, 0, size);
        int idx;
        while ((idx = result.indexOf(path)) > -1) {
            result = result.substring(0, idx) + result.substring(idx + path.length());
        }
        String resuls[] = result.split("\n");
        ArrayList<Registro> lista = new ArrayList<Registro>();
        Registro reg = null;

        for (String s : resuls) {
            reg = new Registro(s.substring(s.indexOf(" and ") + 5, s.lastIndexOf(' ') - 1),
                    s.substring(0, s.indexOf(' ')),
                    Integer.parseInt(s.substring(s.lastIndexOf(' ') + 1, s.length() - 2)));
            if (!reg.origen.equals("\\Sherlock.java") && !reg.destino.equals("\\Sherlock.java")) {
                lista.add(reg);
            }
        }
        Collections.sort(lista);
        for (Registro r : lista) {
            if (r.clase == null || TacticaDetalle.class.isAssignableFrom(r.clase)) {
                System.out.println(r);
            }
        }
        System.out.println("---------------------------------------------");
        for (Registro r : lista) {
            if (r.clase != null && !TacticaDetalle.class.isAssignableFrom(r.clase)) {
                System.out.println(r);
            }
        }
    }
}
